package io.renren.modules.supers.dao;

import io.renren.common.dao.BaseDao;
import io.renren.modules.supers.entity.OrdersEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;

/**
 * 
 *
 * @author Liang Pei 
 * @since 1.0.0 2025-01-03
 */
@Mapper
public interface OrdersDao extends BaseDao<OrdersEntity> {

    @Select("<script>" +
            "SELECT o.phone, o.activity_id, a.title AS activity_title, " +
            "o.business_id, b.name AS business_name, " +
            "o.partner_id, p.name AS partner_name, " +
            "o.partner_business, pb.name AS partner_business_name, " +
            "o.promotion_slug, pr.name AS promotion_name, " +
            "o.status, o.unsubscribe_status, o.start_time, o.end_time " +
            "FROM orders o " +
            "LEFT JOIN activities a ON o.activity_id = a.id " +
            "LEFT JOIN businesses b ON o.business_id = b.id " +
            "LEFT JOIN partners p ON o.partner_id = p.id " +
            "LEFT JOIN partner_businesses pb ON o.partner_business = pb.id " +
            "LEFT JOIN promotions pr ON o.promotion_slug = pr.slug " +
            "WHERE 1=1 " +
            "<if test='phone != null'>AND o.phone = #{phone} </if>" +
            "<if test='activityId != null'>AND o.activity_id = #{activityId} </if>" +
            "<if test='businessId != null'>AND o.business_id = #{businessId} </if>" +
            "<if test='partnerId != null'>AND o.partner_id = #{partnerId} </if>" +
            "<if test='promotion != null'>AND o.promotion_slug = #{promotion} </if>" +
            "<if test='status != null'>AND o.status = #{status} </if>" +
            "<if test='unsubscribeStatus != null'>AND o.unsubscribe_status = #{unsubscribeStatus} </if>" +
            "<if test='startTime != null'>AND o.start_time &gt;= #{startTime} </if>" +
            "<if test='endTime != null'>AND o.end_time &lt;= #{endTime} </if>" +
            "</script>")
    List<T> selectOrders(@Param("phone") String phone,
                         @Param("activityId") Integer activityId,
                         @Param("businessId") Integer businessId,
                         @Param("partnerId") Integer partnerId,
                         @Param("promotion") String promotion,
                         @Param("status") Integer status,
                         @Param("unsubscribeStatus") Integer unsubscribeStatus,
                         @Param("startTime") String startTime,
                         @Param("endTime") String endTime);
}